﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Ebada.Platform.Model;
using DevExpress.XtraGrid.Columns;
using System.Reflection;
using Ebada.Client;
using DevExpress.XtraGrid.Views.Base;
using Ebada.Client.Platform;
using Ebada.UI.Base;
namespace Ebada.Modules.Vehicle
{

    public partial class UCVehicles : DevExpress.XtraEditors.XtraUserControl {
        private GridViewOperation<Vehicles> gridViewOperation;

        public event SendDataEventHandler<Vehicles> FocusedRowChanged;
        public static bool FirstLoad = false;
        public UCVehicles() 
        {
            InitializeComponent();
            gridViewOperation = new GridViewOperation<Vehicles>(gridControl1, gridView1, barManager1,new frmVehiclesEdit());
            gridViewOperation.CreatingObjectEvent +=gridViewOperation_CreatingObjectEvent;
            gridView1.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(gridViewOperation_FocusedRowChanged);
            gridViewOperation.BeforeAdd += new ObjectOperationEventHandler<Vehicles>(gridViewOperation_BeforeAdd);
            gridViewOperation.AfterAdd += new ObjectEventHandler<Vehicles>(gridViewOperation_AfterAdd);
            initColumns();
            InitData();
        }

        void gridViewOperation_AfterAdd(Vehicles obj)
        {
            InitData();
        }

        void gridViewOperation_BeforeAdd(object render, ObjectOperationEventArgs<Vehicles> e)
        {
            int Count=MainHelper.PlatformSqlMap.GetRowCount<Vehicles>("");
            e.Value.VehicleCode =( Count + 1).ToString();
            e.Value.RegDate = DateTime.Now;
            e.Value.InsuranceStartDate = DateTime.Now;
            e.Value.PurchaseDate = DateTime.Now;
            e.Value.RegDate = DateTime.Now;
            e.Value.InsuranceEndDate = DateTime.Now;
        }

        void gridViewOperation_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e) 
        {
            if (FocusedRowChanged != null)
                FocusedRowChanged(gridView1, gridView1.GetFocusedRow() as Vehicles);
        }

        public void HideBar()
        {
            bar1.Visible = false;
            bar3.Visible = false;
        }

       
        /// <summary>
        /// 设置隐藏列
        /// </summary>
        /// 
       public  void initColumns() 
        {
            for (int i = 0; i < gridView1.Columns.Count; i++)
            {
                gridView1.Columns[i].VisibleIndex = -1;
            }
           int m=0;
           gridView1.Columns["VehicleCode"].Caption = "序号";
           gridView1.Columns["VehicleCode"].VisibleIndex = m++;
           gridView1.Columns["VehicleNumber"].Caption = "车牌号";
           gridView1.Columns["VehicleNumber"].VisibleIndex = m++;
           gridView1.Columns["EmployeeName"].Caption = "驾驶员";
           gridView1.Columns["EmployeeName"].VisibleIndex = m++;
           gridView1.Columns["VehicleType"].Caption = "车辆类型";
           gridView1.Columns["VehicleType"].VisibleIndex = m++;
           gridView1.Columns["OldVehicleNumber"].Caption = "原车牌号";
           gridView1.Columns["OldVehicleNumber"].VisibleIndex = m++;
           gridView1.Columns["UsageType"].Caption = "用途";
           gridView1.Columns["UsageType"].VisibleIndex = m++;
           gridView1.Columns["VehicleModel"].Caption = "车辆型号";
           gridView1.Columns["VehicleModel"].VisibleIndex = m++;
           gridView1.Columns["Tonnage"].Caption = "吨位及人数";
           gridView1.Columns["Tonnage"].VisibleIndex = m++;
           gridView1.Columns["RegDate"].Caption = "登记日期";
           gridView1.Columns["RegDate"].VisibleIndex = m++;
           gridView1.Columns["Color"].Caption = "颜色";
           gridView1.Columns["Color"].VisibleIndex = m++;
           gridView1.Columns["Brand"].Caption = "车辆品牌";
           gridView1.Columns["Brand"].VisibleIndex = m++;
           gridView1.Columns["PersonnelAllocationNum"].Caption = "定证编号";
           gridView1.Columns["PersonnelAllocationNum"].VisibleIndex = m++;
           gridView1.Columns["OrgName"].Caption = "车属单位";
           gridView1.Columns["OrgName"].VisibleIndex = m++;
           gridView1.Columns["UsingOrgName"].Caption = "使用单位";
           gridView1.Columns["UsingOrgName"].VisibleIndex = m++;
           gridView1.Columns["VINNum"].Caption = "识别码";
           gridView1.Columns["VINNum"].VisibleIndex = m++;
           gridView1.Columns["DocumentsNum"].Caption = "档案号";
           gridView1.Columns["DocumentsNum"].VisibleIndex = m++;
           gridView1.Columns["EngineNum"].Caption = "发动机号";
           gridView1.Columns["EngineNum"].VisibleIndex = m++;
           gridView1.Columns["PurchaseTax"].Caption = "购置税费";
           gridView1.Columns["PurchaseTax"].VisibleIndex = m++;
           gridView1.Columns["PurchaseDate"].Caption = "购买日期";
           gridView1.Columns["PurchaseDate"].VisibleIndex = m++;
           gridView1.Columns["AcquisitionPrice"].Caption = "购置价格";
           gridView1.Columns["AcquisitionPrice"].VisibleIndex = m++;
           gridView1.Columns["PurchasePrice"].Caption = "购车价";
           gridView1.Columns["PurchasePrice"].VisibleIndex = m++;
           gridView1.Columns["PurchaseTaxNum"].Caption = "购置税证号";
           gridView1.Columns["PurchaseTaxNum"].VisibleIndex = m++;
           gridView1.Columns["InsuranceOrgName"].Caption = "保险单位";
           gridView1.Columns["InsuranceOrgName"].VisibleIndex = m++;
           gridView1.Columns["InsurancePrice"].Caption = "保险金额";
           gridView1.Columns["InsurancePrice"].VisibleIndex = m++;
           gridView1.Columns["InsuranceStartDate"].Caption = "保险期从";
           gridView1.Columns["InsuranceStartDate"].VisibleIndex = m++;
           gridView1.Columns["InsuranceEndDate"].Caption = "保险期至";
           gridView1.Columns["InsuranceEndDate"].VisibleIndex = m++;
           gridView1.Columns["Remark"].Caption = "备注";
           gridView1.Columns["Remark"].VisibleIndex = m++;
           gridView1.Columns["VehicleImage"].Caption = "图片";
           DevExpress.XtraEditors.Repository.RepositoryItemPictureEdit temppic = new DevExpress.XtraEditors.Repository.RepositoryItemPictureEdit();
           temppic.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Zoom;
           gridView1.Columns["VehicleImage"].ColumnEdit = temppic;
           gridView1.Columns["VehicleImage"].VisibleIndex = m++;
           gridView1.Columns["VehicleState"].Caption = "车辆状态";
           gridView1.Columns["VehicleState"].VisibleIndex = m++;
        
          
          
        }
      
    
        /// <summary>
        /// 封装了数据操作的对象
        /// </summary>
        [Browsable(false)]
       public GridViewOperation<Vehicles> GridViewOperation
       {
            get { return gridViewOperation; }
            set { gridViewOperation = value; }
        }
        /// <summary>
        /// 新建对象设置Key值
        /// </summary>
        /// <param name="newobj"></param>
        void gridViewOperation_CreatingObjectEvent(Vehicles newobj)
        {
            //newobj.VehicleID = Guid.NewGuid().ToString();
            newobj.VehicleState = "待命";
           
        }
        public void RefreshDataBysql(string sql)
        {
            gridViewOperation.RefreshData(sql);
        }
        public void InitData()
        {
            gridViewOperation.RefreshData(" where 2>1 order by VehicleCode");
        }
       
       
        private FormBase showControl(UserControl uc)
        {
            FormBase dlg = new FormBase();
           Size newsize=new Size (500,400);
           dlg.Size = newsize;
         
            dlg.Text = "车辆备品信息";
            dlg.Controls.Add(uc);
            uc.Dock = DockStyle.Fill;
            dlg.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            dlg.Show();
            return dlg;
        }
        //查看备品信息
        private void barButtonItem2_ItemClick_1(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0)
            {
                MessageBox.Show("无车辆信息，没有相应备品信息！");
                return;
            }
            string VehicleID = ((Ebada.Platform.Model.Vehicles)(gridView1.GetFocusedRow())).VehicleID;
            UCVehicleParts ucvp = new UCVehicleParts();
            ucvp.VehicleID = VehicleID;
            ucvp.RefreshData(VehicleID);
            showControl(ucvp);
        }

    }
}
